< Summary

Class:GDX.Reflection
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Reflection.cs
Covered lines:112
Uncovered lines:5
Coverable lines:117
Total lines:279
Line coverage:95.7% (112 of 117)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:10
Method coverage:90% (9 of 10)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetDefault(...)0%3.043083.33%
GetType(...)0%33092.86%
InvokeStaticMethod(...)0%4.14081.82%
InvokeMethod(...)0%20400%
SetFieldOrPropertyValue(...)0%4.494068.75%
SetFieldValue(...)0%3.053081.82%
SetPropertyValue(...)0%3.053081.82%
TryGetFieldValue[T](...)0%3.513061.54%
TryGetFieldOrPropertyValue(...)0%5.194057.89%
TryGetPropertyValue[T](...)0%3.513061.54%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Reflection.cs

#LineLine coverage
 1// Copyright (c) 2020-2023 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System;
 6using System.Reflection;
 7
 8namespace GDX
 9{
 10    /// <summary>
 11    ///     A collection of reflection related helper utilities.
 12    /// </summary>
 13    /// <remarks>Torn about the existence of this utility class, yet alone the conditions dictating it.</remarks>
 14    public static class Reflection
 15    {
 16        /// <summary>
 17        ///     <see cref="BindingFlags"/> for a private field.
 18        /// </summary>
 19        public const BindingFlags PrivateFieldFlags = BindingFlags.Instance | BindingFlags.NonPublic;
 20        /// <summary>
 21        ///     <see cref="BindingFlags"/> for a private static.
 22        /// </summary>
 23        public const BindingFlags PrivateStaticFlags = BindingFlags.Static | BindingFlags.NonPublic;
 24        /// <summary>
 25        ///     <see cref="BindingFlags"/> for a public static.
 26        /// </summary>
 27        public const BindingFlags PublicStaticFlags = BindingFlags.Static | BindingFlags.Public;
 28
 29        /// <summary>
 30        ///     Returns the default value for a given type.
 31        /// </summary>
 32        /// <param name="type">A qualified type.</param>
 33        /// <returns>The default value.</returns>
 34        public static object GetDefault(this Type type)
 235        {
 236            if (type.IsClass || !type.IsValueType)
 137            {
 138                return null;
 39            }
 140            return Activator.CreateInstance(type);
 241        }
 42
 43        /// <summary>
 44        ///     Returns a qualified type..
 45        /// </summary>
 46        /// <param name="type">The full name of a type.</param>
 47        /// <returns>A <see cref="System.Type"/> if found.</returns>
 48        public static Type GetType(string type)
 2949        {
 2950            Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
 2951            int loadedAssembliesCount = loadedAssemblies.Length;
 506652            for (int i = 0; i < loadedAssembliesCount; i++)
 253153            {
 253154                Type targetType = loadedAssemblies[i].GetType(type);
 253155                if (targetType != null)
 2756                {
 2757                    return targetType;
 58                }
 250459            }
 260            return null;
 2961        }
 62
 63        /// <summary>
 64        ///     Invokes a known static method.
 65        /// </summary>
 66        /// <param name="type">The explicit type of the static class.</param>
 67        /// <param name="method">The name of the method to invoke.</param>
 68        /// <param name="parameters">Any parameters that should be passed to the method?</param>
 69        /// <param name="flags">The <paramref name="method"/>'s access flags.</param>
 70        /// <returns>An <see cref="object"/> of the return value. This can be null.</returns>
 71        public static object InvokeStaticMethod(string type, string method, object[] parameters = null,
 72            BindingFlags flags = PublicStaticFlags)
 673        {
 674            Type targetType = GetType(type);
 675            if (targetType != null)
 576            {
 577                MethodInfo targetMethod = targetType.GetMethod(method, flags);
 578                if (targetMethod != null)
 479                {
 480                    return targetMethod.Invoke(null, parameters ?? Core.EmptyObjectArray);
 81                }
 182            }
 283            return null;
 684        }
 85
 86        /// <summary>
 87        ///     Invoke a known private method on an object.
 88        /// </summary>
 89        /// <param name="targetObject">The ambiguous object to invoke a method on.</param>
 90        /// <param name="method">The name of the method to invoke.</param>
 91        /// <param name="parameters">Any parameters that should be passed to the method?</param>
 92        /// <param name="flags">The <paramref name="method"/>'s access flags.</param>
 93        /// <returns>An <see cref="object"/> of the return value. This can be null.</returns>
 94        public static object InvokeMethod(object targetObject, string method, object[] parameters = null,
 95            BindingFlags flags = PrivateFieldFlags)
 096        {
 097            Type targetType = targetObject.GetType();
 098            MethodInfo targetMethod = targetType.GetMethod(method, flags);
 099            return targetMethod != null ? targetMethod.Invoke(targetObject, parameters ?? Core.EmptyObjectArray) : null;
 0100        }
 101
 102        /// <summary>
 103        ///     Set the field or property value of a specific <paramref name="targetObject"/>, which may not be
 104        ///     normally accessible.
 105        /// </summary>
 106        /// <param name="targetObject">The instanced object which will have it's field or property value set.</param>
 107        /// <param name="name">The field or property's name to set.</param>
 108        /// <param name="value">The value to set the field or property to.</param>
 109        /// <param name="fieldFlags">The field's access flags.</param>
 110        /// <param name="propertyFlags">The property's access flags.</param>
 111        /// <returns>true/false if the value was set.</returns>
 112        public static bool SetFieldOrPropertyValue(object targetObject, string name, object value,
 113            BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags)
 6114        {
 6115            if (targetObject == null)
 1116                return false;
 117
 5118            Type type = targetObject.GetType();
 5119            FieldInfo f = type.GetField(name, fieldFlags);
 5120            if (f != null)
 2121            {
 2122                f.SetValue(targetObject, value);
 2123                return true;
 124            }
 125
 3126            PropertyInfo p = type.GetProperty(name,propertyFlags);
 3127            if (p != null)
 2128            {
 2129                p.SetValue(targetObject, value);
 2130                return true;
 131            }
 132
 1133            return false;
 6134        }
 135
 136        /// <summary>
 137        ///     Set the field value of a specific <paramref name="targetObject"/>, which may not be normally accessible.
 138        /// </summary>
 139        /// <param name="targetObject">The instanced object which will have it's field value set; use a null value if th
 140        /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param>
 141        /// <param name="name">The field's name to set.</param>
 142        /// <param name="value">The value to set the field to.</param>
 143        /// <param name="flags">The field's access flags.</param>
 144        /// <returns>true/false if the value was set.</returns>
 145        public static bool SetFieldValue(object targetObject, Type type, string name, object value,
 146            BindingFlags flags = PrivateFieldFlags)
 6147        {
 6148            if (type != null)
 5149            {
 5150                FieldInfo field = type.GetField(name, flags);
 5151                if (field != null)
 4152                {
 4153                    field.SetValue(targetObject, value);
 4154                    return true;
 155                }
 1156            }
 2157            return false;
 6158        }
 159
 160        /// <summary>
 161        ///     Set the property value of a specific <paramref name="targetObject"/>, which may not be normally accessib
 162        /// </summary>
 163        /// <param name="targetObject">The instanced object which will have it's property value set; use a null value if
 164        /// <param name="type">The type of the <paramref name="targetObject"/>.</param>
 165        /// <param name="name">The property's name to set.</param>
 166        /// <param name="value">The value to set the property to.</param>
 167        /// <param name="flags">The property's access flags.</param>
 168        /// <returns>true/false if the value was set.</returns>
 169        public static bool SetPropertyValue(object targetObject, Type type, string name, object value,
 170            BindingFlags flags = PrivateFieldFlags)
 4171        {
 4172            if (type != null)
 3173            {
 3174                PropertyInfo property = type.GetProperty(name, flags);
 3175                if (property != null)
 2176                {
 2177                    property.SetValue(targetObject, value);
 2178                    return true;
 179                }
 1180            }
 2181            return false;
 4182        }
 183
 184        /// <summary>
 185        ///     Try to access the field value of a specific <paramref name="targetObject"/>, which may not be normally a
 186        /// </summary>
 187        /// <remarks></remarks>
 188        /// <param name="targetObject">The instanced object which will have it's field value read; use a null value if t
 189        /// <param name="type">The qualified type of the <paramref name="targetObject"/>.</param>
 190        /// <param name="name">The field's name to read.</param>
 191        /// <param name="returnValue">The returned value from the field, the default value if the field was unable to be
 192        /// <param name="flags">The field's access flags.</param>
 193        /// <typeparam name="T">The type of data being read from the field.</typeparam>
 194        /// <returns>true/false if the process was successful.</returns>
 195        public static bool TryGetFieldValue<T>(object targetObject, Type type, string name, out T returnValue, BindingFl
 7196        {
 7197            if (type == null)
 1198            {
 1199                returnValue = default;
 1200                return false;
 201            }
 6202            FieldInfo fieldInfo = type.GetField(name, flags);
 6203            if (fieldInfo == null)
 1204            {
 1205                returnValue = default;
 1206                return false;
 207            }
 5208            returnValue = (T)fieldInfo.GetValue(targetObject);
 5209            return true;
 7210        }
 211
 212        /// <summary>
 213        ///     Try to access the field or property value of a specific <paramref name="targetObject"/>, which may not
 214        ///     be normally accessible.
 215        /// </summary>
 216        /// <remarks>Useful for when you really do not know the <see cref="System.Type"/>.</remarks>
 217        /// <param name="targetObject">The instanced object which will have it's field or property value read.</param>
 218        /// <param name="name">The field or property's name to read.</param>
 219        /// <param name="returnValue">The returned value from the field or property, the default value if the property w
 220        /// <param name="fieldFlags">The field's access flags.</param>
 221        /// <param name="propertyFlags">The property's access flags.</param>
 222        /// <returns>true/false if a value was found.</returns>
 223        public static bool TryGetFieldOrPropertyValue(object targetObject, string name, out object returnValue,
 224            BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags)
 7225        {
 7226            if (targetObject == null)
 1227            {
 1228                returnValue = null;
 1229                return false;
 230            }
 231
 6232            Type type = targetObject.GetType();
 6233            FieldInfo f = type.GetField(name, fieldFlags);
 6234            if (f != null)
 2235            {
 2236                returnValue = f.GetValue(targetObject);
 2237                return true;
 238            }
 239
 4240            PropertyInfo p = type.GetProperty(name,propertyFlags);
 4241            if (p != null)
 3242            {
 3243                returnValue = p.GetValue(targetObject);
 3244                return true;
 245            }
 246
 1247            returnValue = default;
 1248            return false;
 7249        }
 250
 251        /// <summary>
 252        ///     Try to get a property value from <paramref name="targetObject"/>, which may not be normally accessible.
 253        /// </summary>
 254        /// <param name="targetObject">The instanced object which will have it's property value read; use a null value i
 255        /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param>
 256        /// <param name="name">The property's name to read.</param>
 257        /// <param name="returnValue">The returned value from the property, the default value if the property was unable
 258        /// <param name="flags">The property's access flags.</param>
 259        /// <typeparam name="T">The type of data being read from the property.</typeparam>
 260        /// <returns>true/false if the process was successful.</returns>
 261        public static bool TryGetPropertyValue<T>(object targetObject, Type type, string name, out T returnValue, Bindin
 5262        {
 5263            if (type == null)
 1264            {
 1265                returnValue = default;
 1266                return false;
 267            }
 4268            PropertyInfo propertyInfo = type.GetProperty(name, flags);
 4269            if (propertyInfo == null)
 1270            {
 1271                returnValue = default;
 1272                return false;
 273            }
 274
 3275            returnValue = (T)propertyInfo.GetValue(targetObject);
 3276            return true;
 5277        }
 278    }
 279}

Coverage by test methods










































































































































































































































































































































































































































































































































































































































































































Methods/Properties

GetDefault(System.Type)
GetType(System.String)
InvokeStaticMethod(System.String, System.String, System.Object[], System.Reflection.BindingFlags)
InvokeMethod(System.Object, System.String, System.Object[], System.Reflection.BindingFlags)
SetFieldOrPropertyValue(System.Object, System.String, System.Object, System.Reflection.BindingFlags, System.Reflection.BindingFlags)
SetFieldValue(System.Object, System.Type, System.String, System.Object, System.Reflection.BindingFlags)
SetPropertyValue(System.Object, System.Type, System.String, System.Object, System.Reflection.BindingFlags)
TryGetFieldValue[T](System.Object, System.Type, System.String, , System.Reflection.BindingFlags)
TryGetFieldOrPropertyValue(System.Object, System.String, System.Object&, System.Reflection.BindingFlags, System.Reflection.BindingFlags)
TryGetPropertyValue[T](System.Object, System.Type, System.String, , System.Reflection.BindingFlags)